﻿// <copyright file="VoteDao.cs" company="NTalent">
// Copyright (c) 2010, NTalent, Inc. All rights reserved
// </copyright>
// <author>卢俊坤</author>
// <author>王彦博 符茂松</author>

namespace Net.Ntalent.Action50.Core.Dao
{
    using System;
    using System.Linq;
    using Net.Ntalent.Action50.DataAccess;

    /// <summary>
    /// 对于T_VOTE_RECODE实体的数据库操作实体
    /// </summary>
    /// <author>王彦博</author>
    /// <copyright>
    /// Copyright (c) 2010, NTalent, Co. Ltd. All rights reserved.
    /// </copyright>
    /// <version>1.0.0</version>  
    public class VoteDao : IDisposable
    {
        /// <summary>
        /// 数据库操作实体类，readonly, 在构造函数初始化，无getter/setter
        /// </summary>
        private Action50DataContext context;

        /// <summary>
        /// 构造方法
        /// </summary>
        public VoteDao()
        {
            this.context = new Action50DataContext();
        }

        /// <summary>
        /// 将传入的T_VOTE_RECODE实体进行持久化.
        /// </summary>
        /// <param name="action">需要新建的T_VOTE_RECODE实体</param>
        public void Create(T_VOTE_RECODE vote)
        {
            this.context.T_VOTE_RECODE.InsertOnSubmit(vote);
            this.context.SubmitChanges();
        }

        /// <summary>
        /// 取得某IP地址的投票次数
        /// </summary>
        /// <param name="ipAdress"></param>
        /// <returns></returns>
        public int Count(string ipAdress)
        {
            return this.context.T_VOTE_RECODE.Count(w => w.USER_IP == ipAdress);
        }

        /// <summary>
        /// 返回指定ip在指定action中的投票数
        /// </summary>
        /// <param name="ipAddress">指定的ip地址</param>
        /// <param name="actionId">指定的action</param>
        ///指定ip在指定action中的投票数
        public int Count(string ipAddress, long actionId)
        {
            return this.context.T_VOTE_RECODE.Count(w => w.USER_IP == ipAddress && w.ACTION_ID == actionId);
        }

        /// <summary>
        /// 删除所有投票记录
        /// </summary>
        public void DeleteAll()
        {
            IQueryable<T_VOTE_RECODE> allVotes = this.context.T_VOTE_RECODE.Where(w => true);
            this.context.T_VOTE_RECODE.DeleteAllOnSubmit(allVotes);
            this.context.SubmitChanges();
        }

        //实现Idisposable接口，暂不执行任何操作
        public void Dispose()
        {
        }
    }
}
